package com.opoc.service.Impl;

import com.opoc.controller.ReportController;
import com.opoc.mapper.EnterpriseMapper;
import com.opoc.mapper.ReportMapper;
import com.opoc.pojo.*;
import com.opoc.service.ReportService;
import com.opoc.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

import static com.opoc.utils.FileUtils.getFileExtension;

/**
 * @program: opoc
 * @description: 检测报告
 * @author: lyx
 * @create: 2023-10-26 14:46
 */
@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    /*
    * 获取检测报告基本信息
    * */
    @Autowired
    ReportMapper reportMapper;
    @Autowired
    EnterpriseMapper enterpriseMapper;

    @Override
    public List<Report> getReportInfo(String company_id, String company_name, String contacts) {
        return reportMapper.getReportInfo(company_id,company_name,contacts);
    }


    /**
     * 添加检测报告
     * */
    @Override
    public String addReport(String company_id, String date, String people, String report_file) {
        if(!enterpriseMapper.validEnterpriseById(company_id)){
            return "选择的公司不存在，请到企业管理添加企业";
        }
        else {
            reportMapper.addReport(company_id, date, people, report_file);
            return "access";
        }
    }

    /*
    * 删除报告
    * */
    @Override
    public String deleteReport(List<String> company_ids) {
        System.out.println(company_ids);
        for (String company_id : company_ids){
            System.out.println(company_id);
            if (company_id != null){
                FileUtils.deleteFile(reportMapper.URLfindId(company_id));
                reportMapper.deleteReport(company_id);
            } else {
                return "报告不存在";
            }
        }
        return "access";
    }
  /*
   *上传检测报告
   *  */
@Override
 public Map<String,String> uploadFile (String company_id, MultipartFile report_file){
     String company_name=reportMapper.findById(company_id);
     Map<String, String> result = new HashMap<>();
     String report_file_path = FileUtils.uploadFiles(report_file,  company_name);
     reportMapper.uploadFile(company_id,report_file_path);
    report_file_path=FileUtils.getURLFromPath(report_file_path);
     result.put("report_file", report_file_path);

     return result;
 }
/*
* 下载检测报告
* */
    @Override
    public ResponseEntity<byte[]> downloadFile(String company_id) throws IOException {
        String report_file_path=reportMapper.URLfindId(company_id);
        String report_file_url=FileUtils.getURLFromPath(report_file_path);
        System.out.println(report_file_url);
        String filename=FileUtils.getFilename(report_file_url);
        ResponseEntity<byte[]> responseEntity=FileUtils.downloadFileByUrl(report_file_url,filename);
        return responseEntity;

    }


}
